package com.jlastu.znb.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.database.DBHelper;
import com.jlastu.znb.entity.User;
import com.jlastu.znb.presenter.MinePresenter;
import com.jlastu.znb.utils.SpUtils;

import org.jetbrains.annotations.Nullable;

public class MineFragment extends BaseFragment implements View.OnClickListener, MineContract.IMineView {

   private TextView mTvName;
   private RelativeLayout mSettingLayout;
   private MineContract.IMinePresenter mMinePresenter;
   private SpUtils mSpUtils;
   @Override
   protected int getLayoutId() {
      return R.layout.fragment_mine;
   }

   protected void initView() {
      mSettingLayout = mView.findViewById(R.id.settingView);

   }

   @Override
   protected void initListener() {
      mSettingLayout.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.settingView) {
         Intent intent = new Intent(mContext, SettingsActivity.class);
         startActivity(intent);
      }


   }


   @Override
   public void onResume() {
      super.onResume();
      // 从SharedPreferences获取当前登录用户的ID
      int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1); // 使用-1作为未登录的默认值
      if (loginId != -1) {
         // 根据ID查询当前登录用户的信息
         mMinePresenter.getLoginUser(loginId);
      }
      else {
         mTvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
               if (userId == -1){

                  Intent intent = new Intent(mContext, LoginActivity.class);
                  startActivity(intent);
                  // 例如，可以设置一个TextView来提示用户登录

               }
            }
         });
         mTvName.setText("点击登录");
      }
   }




   @Override
   public void showUserInfo(User user) {
      if (user != null) {
         // 用户信息存在，更新UI
         mTvName.setText(user.getUserName());
      } else {
         // 用户信息不存在或查询失败，显示错误信息
         mTvName.setText("用户信息获取失败，请重新登录");
         // 可能需要提示用户重新登录
      }
   }
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

      View view = inflater.inflate(R.layout.fragment_mine, container, false);
      mTvName = view.findViewById(R.id.user_name);
      mSettingLayout=view.findViewById(R.id.settingView);
      mSpUtils = new SpUtils(getContext());
      initListener();
      DBHelper dbHelper=new DBHelper(getContext());
      updateUserInfo(dbHelper);

      //购物车按钮
      LinearLayout lessonLinearLayout = view.findViewById(R.id.gwccc);
      lessonLinearLayout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),com.jlastu.znb.gwc.CartActivity.class);
            startActivity(intent);
         }
      });

      //收藏
      LinearLayout scLinearLayout = view.findViewById(R.id.scan);
      scLinearLayout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),com.jlastu.znb.sc.scActivity.class);
            startActivity(intent);
         }
      });

      //待付款
      LinearLayout dfk = view.findViewById(R.id.bbbuuuyyy);
      dfk.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),com.jlastu.znb.buy.buyActivity.class);
            startActivity(intent);
         }
      });
      //待收货
      LinearLayout dsh = view.findViewById(R.id.ddsshh);
      dsh.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),com.jlastu.znb.dfh.dfhActivity.class);
            startActivity(intent);
         }
      });
      //足迹
      LinearLayout zj = view.findViewById(R.id.zzjj);
      zj.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),com.jlastu.znb.zj.zjActivity.class);
            startActivity(intent);
         }
      });
      return view;
   }



   private void updateUserInfo(DBHelper dbHelper) {
      int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
      if (userId != -1) {
         String userName = getUserNameFromDb(dbHelper, userId); // 传递DBHelper实例
         if (userName != null) {
            mTvName.setText(userName);
         } else {
            mTvName.setText("用户信息获取失败");
         }
      } else {
         mTvName.setText("请登录");
      }
   }

   @SuppressLint("Range")
   private String getUserNameFromDb(DBHelper dbHelper,int userId) {

        String userName = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = { "user_name" };
        String selection = "_id = ?";
        String[] selectionArgs = { String.valueOf(userId) };
        Cursor cursor = db.query("user", columns, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            userName = cursor.getString(cursor.getColumnIndex("user_name"));
        }
        cursor.close();
        return userName;

   }
   @Override
   public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mSpUtils = new SpUtils(getContext());
      // 初始化 presenter，确保传入当前的视图接口实例和自己
      mMinePresenter = new MinePresenter(this, getContext());
      // 监听点击事件

   }
}
